package com.ymatou.iapi.couponservice.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.UserReceivePackageBean;
import com.ymatou.iapi.couponservice.parameter.UserReceiveResultInfo;
import com.ymatou.iapi.couponservice.parameter.UserReceiveResultList;
import com.ymatou.iapi.couponservice.service.UserReceivePackageCall;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;

import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;

import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.business.service.CouponServiceMCallService;

import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.model.YmtCouponVirtualRelation;

import com.ymttest.database.sqlwapper.YmtCouponActivityWapper;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;

import com.ymttest.utils.TestDataManager;

@TestSuite
public class Ts_UserReceivePackage {
	private static UserReceivePackageBean userreceivepackageBean;
	private static UserReceivePackageCall userreceivepackageCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户领取优惠券用例");
	}

	@Before
	public void caseUp() {
		userreceivepackageBean=new UserReceivePackageBean();
		userreceivepackageCall=new UserReceivePackageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(userreceivepackageCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_001() {
		Logger.start(true,"用户领取优惠券-平台链接-多条");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			//Thread.sleep(6000);
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}

			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})    
	@TestCase
	public void Tc_UserReceivePackage_002() {
		Logger.start(true,"用户领取优惠券-平台链接-单条");
		try {
			// 设置三个batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());	
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_003() {
		Logger.start(true,"用户领取优惠券-商家-多条");
		try {
			// 设置三个batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			userreceivepackageBean = couponCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_004() {
		Logger.start(true,"用户领取优惠券-商家-单条");
		try {
			// 设置三个batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			userreceivepackageBean = couponCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_005() {
		Logger.start(true,"用户领取优惠券-平台链接+商家券");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();

			// 平台优惠券-链接
			CreateCouponBean createcouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();		

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());	
			batchCodes.add(couponCallService.createPlatformCoupon(createcouponBean).getBatchCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			userreceivepackageBean = couponCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_006() {
		Logger.start(true,"反例-请传入用户ID");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
	
			userreceivepackageBean = couponCallService.setUserReceivePackageBean(batchCodes);
			userreceivepackageBean.setUserId(null);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		

			Logger.verifyEquals("null", userreceivepackageCall.getSellerId(), "验证返回SellerId");
			Logger.verifyEquals("1", userreceivepackageCall.getCode(), "验证返回code");
			Logger.verifyEquals("参数错误|userId:请传入用户ID", userreceivepackageCall.getMessage(), "验证返回Message");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_007() {
		Logger.start(true,"反例-请传入批次编号");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();

			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		

			Logger.verifyEquals("null", userreceivepackageCall.getSellerId(), "验证返回SellerId");
			Logger.verifyEquals("1", userreceivepackageCall.getCode(), "验证返回code");
			Logger.verifyEquals("参数错误|batchCodes:请传入批次编号", userreceivepackageCall.getMessage(), "验证返回Message");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_008() {
		Logger.start(true,"反例-请传入批次编号");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			List<String> batchCodes=new ArrayList<>();
			batchCodes.add("1223344");
			batchCodes.add("123344");
			batchCodes.add("1233844");
			batchCodes.add("12233944");
		//	userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageBean.setUserId(1111);
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		

			Logger.verifyEquals("null", userreceivepackageCall.getSellerId(), "验证返回SellerId");
			Logger.verifyEquals("1", userreceivepackageCall.getCode(), "验证返回code");
			Logger.verifyEquals("参数错误|batchCodes:请传入批次编号", userreceivepackageCall.getMessage(), "验证返回Message");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_009() {
		Logger.start(true,"反例-请传入批次编号");
		try {
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(null);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		

			Logger.verifyEquals("null", userreceivepackageCall.getSellerId(), "验证返回SellerId");
			Logger.verifyEquals("1", userreceivepackageCall.getCode(), "验证返回code");
			Logger.verifyEquals("参数错误|batchCodes:请传入批次编号", userreceivepackageCall.getMessage(), "验证返回Message");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_010() {
		Logger.start(true,"正例-JIRACONN-806传入虚拟券领取");
		try {

			List<String> batchCodes = new ArrayList<>();
			
			CouponCallService couponCallServcie = new CouponCallService();
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());	
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			Map<String, String> virtualCouponCodes = new HashMap() ;
			
			String code = TestDataManager.getRandomNum(10);
			virtualCouponCodes.put(createCouponCall.getBatchCode(), code);
			userreceivepackageBean.setVirtualCouponCodes(virtualCouponCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userreceivepackageBean.getUserId());
			
			// 验证Ymt_CouponVirtualRelation表
			List<YmtCouponVirtualRelation> couponVirtualRelations = new YmtCouponActivityWapper().selectCouponVirtualRelationByCouponCodeAndVirtualCouponCode(ymtCouponPrivateUserBounds.get(0).getsCouponCode(), code);
			Logger.verifyEquals(1, couponVirtualRelations.size(), "验证Ymt_CouponVirtualRelation生成一条记录");

			if(couponVirtualRelations.size()==1)
			{
				YmtCouponVirtualRelation relation = couponVirtualRelations.get(0);
				
				Logger.verifyEquals(false, relation.getIsUsed(), "验证Ymt_CouponVirtualRelation中IsUsed");
				Logger.verifyEquals(false, relation.getIsCancel(), "验证Ymt_CouponVirtualRelation中IsCancel");
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_UserReceivePackage_011() {
		Logger.start(false,"反例-JIRACONN-806传入虚拟券领取-已领用");
		try {

			List<String> batchCodes = new ArrayList<>();
			
			CouponCallService couponCallServcie = new CouponCallService();
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
//			CreateCouponBean createcouponBean = couponCallServcie.setDefaultPlatformLinkCreateCouponBean();
//			CreateCouponCall createCouponCall = couponCallServcie.createPlatformCoupon(createcouponBean);
			
//			batchCodes.add(createCouponCall.getBatchCode());	
//			
//			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			userreceivepackageBean.setUserId(20238699);
			Map<String, String> virtualCouponCodes = new HashMap() ;
			
			YmtCouponVirtualRelation relation = new YmtCouponActivityWapper().selectTop1CouponVirtualRelation().get(0);
			
			String couponCode = relation.getCouponCode();
			
			int ibatchid=new YmtCouponWapper().selectBysCouponCode(couponCode).get(0).getiBatchId();
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(ibatchid);
			
	batchCodes.add(batch.getsBatchCode());	
			
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			
			YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(relation.getCouponCode()).get(0);
//			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(coupon.getiBatchId());
			
			virtualCouponCodes.put(batch.getsBatchCode(), relation.getVirtualCouponCode());
			userreceivepackageBean.setVirtualCouponCodes(virtualCouponCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
		
			Logger.verifyEquals("3", userreceivepackageCall.getCode(), "验证返回code");

			Logger.verifyEquals("系统异常，请稍后重试", userreceivepackageCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = userreceivepackageCall.getUserReceiveResultList();
			
//			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
//			
//			for(int i=0;i<list.size();i++)
//			{
//				UserReceiveResultInfo item = list.get(i);
//				Logger.verifyEquals(userreceivepackageBean.getUserId(), item.getUserId(), "验证返回UserId");
//				Logger.verifyEquals("", item.getBatchCode(), "验证返回BatchCode");
//				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
//				Logger.verifyEquals(12, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
//			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_012() {
		Logger.start(true,"正例-JIRACONN-806传入虚拟券领取-使用后IsUsed=1");
		try {

			List<String> batchCodes = new ArrayList<>();
			
			CouponCallService couponCallServcie = new CouponCallService();
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());	
			
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			Map<String, String> virtualCouponCodes = new HashMap() ;
			
			String code = TestDataManager.getRandomNum(10);
			virtualCouponCodes.put(createCouponCall.getBatchCode(), code);
			userreceivepackageBean.setVirtualCouponCodes(virtualCouponCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userreceivepackageBean.getUserId());
		
			new CouponCallService().useCoupon(userreceivepackageBean.getUserId(), ymtCouponPrivateUserBounds.get(0).getsCouponCode());
			
			// 验证Ymt_CouponVirtualRelation表
						List<YmtCouponVirtualRelation> couponVirtualRelations = new YmtCouponActivityWapper().selectCouponVirtualRelationByCouponCodeAndVirtualCouponCode(ymtCouponPrivateUserBounds.get(0).getsCouponCode(), code);
						Logger.verifyEquals(1, couponVirtualRelations.size(), "验证Ymt_CouponVirtualRelation生成一条记录");

						if(couponVirtualRelations.size()==1)
						{
							YmtCouponVirtualRelation relation = couponVirtualRelations.get(0);
							
							Logger.verifyEquals(true, relation.getIsUsed(), "验证Ymt_CouponVirtualRelation中IsUsed");
							Logger.verifyEquals(false, relation.getIsCancel(), "验证Ymt_CouponVirtualRelation中IsCancel");
						}
					
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_013() {
		Logger.start(true,"用户领取优惠券-平台链接-非链接");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());

			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			Logger.verifyEquals("0", userreceivepackageCall.getCode(), "验证返回code");

			Logger.verifyEquals("操作成功！", userreceivepackageCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = userreceivepackageCall.getUserReceiveResultList();
			
			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
			
			List<String> returnbatchCode = new ArrayList<>();
			int num = list.size();
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);
				Logger.verifyEquals(userreceivepackageBean.getUserId(), item.getUserId(), "验证返回UserId");
				returnbatchCode.add(item.getBatchCode());
				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
				Logger.verifyEquals(19, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
				Logger.verifyEquals("批次不能用于领取", item.getRemark(), "验证返回Remark");

				YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(item.getBatchCode()).get(0);
				
				List<YmtCouponPrivateUserBound> bounds = new YmtCouponPrivateUserBoundWapper()
						.selectByBatchIdAndUserId(couponBatch.getiBatchId(), item.getUserId());
				
				 Logger.verifyEquals(0, bounds.size(),
			                "验证YmtCouponPrivateUserBound表没有数据生成");
				 
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_014() {
		Logger.start(true,"用户领取优惠券-平台链接-不在领取时间范围");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();

	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -10); // 设置为前一天
	        dBefore = calendar1.getTime(); // 得到前一天的时间

	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.DAY_OF_MONTH, -1); // 设置为后十天
	        dAfter = calendar2.getTime(); // 得到前一天的时间
	        
			for (String batchCode:batchCodes) {

				new YmtCouponBatchWapper().setAcquireStartTime(batchCode, dBefore);
				new YmtCouponBatchWapper().setAcquireEndTime(batchCode, dAfter);
			}

			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
		
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			Logger.verifyEquals("0", userreceivepackageCall.getCode(), "验证返回code");

			Logger.verifyEquals("操作成功！", userreceivepackageCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = userreceivepackageCall.getUserReceiveResultList();
			
			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
			
			List<String> returnbatchCode = new ArrayList<>();
			int num = list.size();
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);
				Logger.verifyEquals(userreceivepackageBean.getUserId(), item.getUserId(), "验证返回UserId");
				returnbatchCode.add(item.getBatchCode());
				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
				Logger.verifyEquals(14, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
				Logger.verifyEquals("超出领取结束时间", item.getRemark(), "验证返回Remark");

				YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(item.getBatchCode()).get(0);
				
				List<YmtCouponPrivateUserBound> bounds = new YmtCouponPrivateUserBoundWapper()
						.selectByBatchIdAndUserId(couponBatch.getiBatchId(), item.getUserId());
				
				 Logger.verifyEquals(0, bounds.size(),
			                "验证YmtCouponPrivateUserBound表没有数据生成");
				 
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_015() {
		Logger.start(true,"用户领取优惠券-平台链接-批次不能用于领取");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForConversion();
			
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
		
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
		
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			Logger.verifyEquals("0", userreceivepackageCall.getCode(), "验证返回code");

			Logger.verifyEquals("操作成功！", userreceivepackageCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = userreceivepackageCall.getUserReceiveResultList();
			
			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
			
			List<String> returnbatchCode = new ArrayList<>();
			int num = list.size();
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);
				Logger.verifyEquals(userreceivepackageBean.getUserId(), item.getUserId(), "验证返回UserId");
				returnbatchCode.add(item.getBatchCode());
				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
				Logger.verifyEquals(19, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
				Logger.verifyEquals("批次不能用于领取", item.getRemark(), "验证返回Remark");

				YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(item.getBatchCode()).get(0);
				
				List<YmtCouponPrivateUserBound> bounds = new YmtCouponPrivateUserBoundWapper()
						.selectByBatchIdAndUserId(couponBatch.getiBatchId(), item.getUserId());
				
				 Logger.verifyEquals(0, bounds.size(),
			                "验证YmtCouponPrivateUserBound表没有数据生成");
				 
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_016() {
		Logger.start(true,"用户领取优惠券-平台链接-多条-coupon相对立即生效的优惠券截止时间至23:59:59S-JIRACONN-13324");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			createcouponBean.setEffectiveType(1);
			createcouponBean.setEffectiveValidType(0);
			createcouponBean.setEffectiveDays(1);
			
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}

			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
			UserReceiveResultList userReceiveResultList = userreceivepackageCall.getUserReceiveResultList();

			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
		
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);				

				YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(item.getCouponCode()).get(0);
				
				Date validStart = coupon.getdValidStart();
				Calendar calendar1 = Calendar.getInstance(); // 得到日历
				calendar1.setTime(validStart);// 把当前时间赋给日历
				calendar1.add(Calendar.DAY_OF_MONTH, createcouponBean.getEffectiveDays()); // 设置为前一天
				Date validEnd = calendar1.getTime(); // 得到前一天的时间
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
				
				Logger.verifyEquals(sdf.format(validEnd).substring(0, 10)+" 23:59:59.000", sdf.format(coupon.getdValidEnd()), "验证Coupon表ValidEnd");

			}

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_017() {
		Logger.start(true,"用户领取优惠券-商家-多条-JIRACONN-15533 可以领取买手还未生效的优惠券");
		try {
			// 设置三个batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			
			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, 1); // 设置为前一天
			dBefore = calendar1.getTime(); // 得到前一天的时间

			couponBatchBean.setEffectiveStartDate(dBefore);
			
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			batchCodes.add(couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			for (String batchCode:batchCodes) {

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			userreceivepackageBean = couponCallService.setUserReceivePackageBean(batchCodes);
			
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			verifyReturn(receiveCountlist,settingIdlist);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UserReceivePackage_018() {
		Logger.start(true,"用户领取优惠券-平台链接-JIRACONN-15533 不在领取时间范围");
		try {
			// 设置batchcode
			List<String> batchCodes = new ArrayList<>();
			
			CouponServiceMCallService CouponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createcouponBean = CouponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			
			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, 1); // 设置为前一天
			dBefore = calendar1.getTime(); // 得到前一天的时间

			createcouponBean.setAcquireStartTime(dBefore);
			
			CreateCouponCall createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
			
			createCouponCall = CouponServiceMCallService.createPlatformCoupon(createcouponBean);
			batchCodes.add(createCouponCall.getBatchCode());
		
			userreceivepackageBean = CouponServiceMCallService.setUserReceivePackageBean(batchCodes);
		
			userreceivepackageCall.setData(userreceivepackageBean);
			userreceivepackageCall.callService();		
			
			Logger.verifyEquals("0", userreceivepackageCall.getCode(), "验证返回code");

			Logger.verifyEquals("操作成功！", userreceivepackageCall.getMessage(), "验证返回Message");

			UserReceiveResultList userReceiveResultList = userreceivepackageCall.getUserReceiveResultList();
			
			List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
			
			List<String> returnbatchCode = new ArrayList<>();
			int num = list.size();
			for(int i=0;i<list.size();i++)
			{
				UserReceiveResultInfo item = list.get(i);
				Logger.verifyEquals(userreceivepackageBean.getUserId(), item.getUserId(), "验证返回UserId");
				returnbatchCode.add(item.getBatchCode());
				Logger.verifyEquals(false, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
				Logger.verifyEquals(13, item.getReceiveFailReason(), "验证返回ReceiveFailReason");
				Logger.verifyEquals("未到领取开始时间", item.getRemark(), "验证返回Remark");

				YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(item.getBatchCode()).get(0);
				
				List<YmtCouponPrivateUserBound> bounds = new YmtCouponPrivateUserBoundWapper()
						.selectByBatchIdAndUserId(couponBatch.getiBatchId(), item.getUserId());
				
				 Logger.verifyEquals(0, bounds.size(),
			                "验证YmtCouponPrivateUserBound表没有数据生成");
				 
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn(List<Integer> receiveCountlist, List<Integer> settingIdlist)
	{		
		Logger.verifyEquals("0", userreceivepackageCall.getCode(), "验证返回code");

		Logger.verifyEquals("操作成功！", userreceivepackageCall.getMessage(), "验证返回Message");

		UserReceiveResultList userReceiveResultList = userreceivepackageCall.getUserReceiveResultList();
		
		List<UserReceiveResultInfo> list = userReceiveResultList.getUserReceiveResultList();
		
		List<String> returnbatchCode = new ArrayList<>();
		int num = list.size();
		for(int i=0;i<list.size();i++)
		{
			UserReceiveResultInfo item = list.get(i);
			Logger.verifyEquals(userreceivepackageBean.getUserId(), item.getUserId(), "验证返回UserId");
			returnbatchCode.add(item.getBatchCode());
			Logger.verifyEquals(true, item.getIsReceiveSuccess(), "验证返回IsReceiveSuccess");
			Logger.verifyEquals(0, item.getReceiveFailReason(), "验证返回ReceiveFailReason");

			YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(item.getBatchCode()).get(0);
			YmtCouponSetting couponSetting = new YmtCouponSettingWapper()
					.selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);

			List<YmtCouponPrivateUserBound> bounds = new YmtCouponPrivateUserBoundWapper()
					.selectByBatchIdAndUserId(couponBatch.getiBatchId(), item.getUserId());
			
			 Logger.verifyEquals(bounds.get(0).getsCouponCode(), item.getCouponCode(),
		                "验证返回CouponCode");
			 
			int remainReceiveTimes = couponSetting.getiMaxUseTimePerUser() - bounds.size();

			Logger.verifyEquals(remainReceiveTimes, item.getRemainReceiveTimes(), "验证返回RemainReceiveTimes");
			
		}

		Logger.verifyEquals(true, isListEaqual(returnbatchCode, userreceivepackageBean.getBatchCodes()),
				"验证返回batchcode与传入一致");

		for (int i = 0; i < settingIdlist.size(); i++) {
			YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(settingIdlist.get(i))
					.get(0);
			Logger.verifyEquals(1, setting.getiReceiveCount() - receiveCountlist.get(i),
					"验证返回Ymt_CouponSetting表iSettingId=" + settingIdlist.get(i) + "的记录中iReceiveCount多了1");
		}

		for (String batchCode : userreceivepackageBean.getBatchCodes()) {
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

			List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper()
					.selectByBatchIdAndUserId(batch.getiBatchId(), userreceivepackageBean.getUserId());
			Logger.verifyEquals(1, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound生成一条记录");

			List<YmtCoupon> coupons = new YmtCouponWapper()
					.selectBysCouponCode(ymtCouponPrivateUserBounds.get(0).getsCouponCode());
			Logger.verifyEquals(1, coupons.size(), "验证返回YmtCoupon生成一条记录");

		}
	}
	
	private boolean isListEaqual(List<String> list1,List<String> list2)
	{
		for(String str:list1)
		{
			if(!list2.contains(str))
				return false;
		}
		
		for(String str:list2)
		{
			if(!list1.contains(str))
				return false;
		}
		
		return true;
	}
}
